package cn.admobiletop.adsuyi.adapter.gdt.loader;

import android.app.Activity;

import com.qq.e.ads.interstitial2.UnifiedInterstitialAD;

import cn.admobiletop.adsuyi.ad.ADSuyiFullScreenVodAd;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterLoader;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterParams;
import cn.admobiletop.adsuyi.ad.data.ADSuyiPlatformPosId;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiFullScreenVodAdListener;
import cn.admobiletop.adsuyi.adapter.gdt.listener.FullScreenVodAdListener;
import cn.admobiletop.adsuyi.util.ADSuyiAdUtil;

/**
 * @author ciba
 * @description 全屏视频广告加载器
 *              需要实现ADSuyiAdapterLoader<ADSuyiFullScreenVodAd, ADSuyiFullScreenVodAdListener>
 * @date 2020/4/8
 */
public class FullScreenVodAdLoader implements ADSuyiAdapterLoader<ADSuyiFullScreenVodAd, ADSuyiFullScreenVodAdListener> {
    private UnifiedInterstitialAD unifiedInterstitialAD;
    private FullScreenVodAdListener fullScreenVodAdListener;

    /**
     * 加载全屏视频广告
     * @param fullScreenVodAd
     * @param adapterParams
     * @param adListener
     */
    @Override
    public void loadAd(ADSuyiFullScreenVodAd fullScreenVodAd, ADSuyiAdapterParams adapterParams, ADSuyiFullScreenVodAdListener adListener) {
        if (!ADSuyiAdUtil.isReleased(fullScreenVodAd)
                && adapterParams != null
                && adapterParams.getPlatform() != null
                && adapterParams.getPlatformPosId() != null
                && adListener != null) {
            ADSuyiPlatformPosId adSuyiPlatformPosId = adapterParams.getPlatformPosId();
            // 广告加载的Activity
            Activity activity = fullScreenVodAd.getActivity();
            // 广告平台广告位ID
            String platformPosId = adSuyiPlatformPosId.getPlatformPosId();

            loadFullScreenAd(activity, platformPosId, adListener);
        }
    }

    /**
     * 加载广点通全屏视频广告
     * @param activity      广告加载广告的Activity
     * @param platformPosId 广告平台广告位ID
     * @param adListener    广告监听回调
     */
    private void loadFullScreenAd(Activity activity, String platformPosId, ADSuyiFullScreenVodAdListener adListener) {
        // 构造全屏视频监听类，实现广点通全屏视频广告回调接口
        fullScreenVodAdListener = new FullScreenVodAdListener(platformPosId, adListener);

        // 以下是广点通加载广告逻辑
        // 构造广点通UnifiedInterstitialAD对象，传入必要的参数
        unifiedInterstitialAD = new UnifiedInterstitialAD(activity
                , platformPosId
                , fullScreenVodAdListener);

        // 向封装的全屏视频监听类中传入unifiedInterstitialAD，用于返回给用户进行全屏视频的播放
        fullScreenVodAdListener.setUnifiedInterstitialAD(unifiedInterstitialAD);
        // 加载广点通全屏视频广告
        unifiedInterstitialAD.loadFullScreenAD();
    }

    @Override
    public void onResumed() {
        // 目前不做处理
    }

    @Override
    public void onPaused() {
        // 目前不做处理
    }

    /**
     * ADSuyi广告对象被释放会调用该方法
     */
    @Override
    public void release() {
        if (unifiedInterstitialAD != null) {
            unifiedInterstitialAD.destroy();
            unifiedInterstitialAD = null;
        }
        if (fullScreenVodAdListener != null) {
            fullScreenVodAdListener.release();
            fullScreenVodAdListener = null;
        }
    }
}
